package com.hctforgreen.greenservice.ui.widget;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.TerminalLearningMaterialsActivity;
import com.hctforgreen.greenservice.model.MessageTopThirdEntity;
import com.hctforgreen.greenservice.ui.adapter.MessageTopThirdLstAdapter;
import com.hctforgreen.greenservice.ui.adapter.TerminalDataTimeListAdapter;

/**
 * 
 * @author 390694 终端培训---时间选择下拉框
 * 
 */
public class TerminalDataTimePopupView extends PopupWindow {

	private View mConvertView;
	private Activity mActivity;

	private MessageTopThirdEntity mSelectTimeEntity;

	private ArrayList<MessageTopThirdEntity> mEntities = new ArrayList<MessageTopThirdEntity>();

	private TerminalLearningMaterialsActivity mSelf2;

	public TerminalDataTimePopupView(Activity mActivity,
			MessageTopThirdEntity mSelectTimeEntity,
			TerminalLearningMaterialsActivity mSelf2) {

		super(mActivity);
		this.mActivity = mActivity;

		this.mSelectTimeEntity = mSelectTimeEntity;
		this.mSelf2 = mSelf2;

		initDataList();
		initWindow();

	}

	/**
	 * 
	 * @param type
	 *            0:最近一周；1：最近一个月；2：最近一年
	 * @return
	 */
	private String getDateFrom(int type) {
		java.util.Date date = new java.util.Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		long lTime;
		if (type == 0) {
			lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 7;
			date.setTime(lTime * 1000);
			String sDateString = format.format(date);
			return sDateString;
		} else if (type == 1) {
			lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 30;
			date.setTime(lTime * 1000);
			String sDateString = format.format(date);
			return sDateString;
		} else if (type == 2) {
			lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 365;
			date.setTime(lTime * 1000);
			String sDateString = format.format(date);
			return sDateString;
		} else {
			return "";
		}
	}

	private void initDataList() {
		MessageTopThirdEntity timeEntity = new MessageTopThirdEntity();
		timeEntity.name = mActivity.getResources().getString(
				R.string.terminal_train_one_week);
		timeEntity.dateFrom = getDateFrom(0);
		timeEntity.dateType = "0";
		mEntities.add(timeEntity);

		timeEntity = new MessageTopThirdEntity();
		timeEntity.name = mActivity.getResources().getString(
				R.string.terminal_train_one_meonth);
		timeEntity.dateFrom = getDateFrom(1);
		timeEntity.dateType = "1";
		mEntities.add(timeEntity);

		timeEntity = new MessageTopThirdEntity();
		timeEntity.name = mActivity.getResources().getString(
				R.string.terminal_train_one_year);
		timeEntity.dateFrom = getDateFrom(2);
		timeEntity.dateType = "2";
		mEntities.add(timeEntity);
	}

	private void initWindow() {
		mConvertView = getLayout();

		this.setContentView(mConvertView);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
		this.setFocusable(true);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		loadDataList();
	}

	private void loadDataList() {
		final ListView firstLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		renderDataLst(firstLv, mEntities);
	}

	private void renderDataLst(ListView firstLv,
			List<MessageTopThirdEntity> entities) {

		if (entities == null || entities.size() == 0) {
			return;
		}

		new TerminalDataTimeListAdapter(firstLv, mActivity, entities,
				mSelectTimeEntity, this);
	}

	private View getLayout() {
		View layout = mActivity.getLayoutInflater().inflate(
				R.layout.popup_message_top_list, null);
		return layout;
	}

	public void updateWindow(MessageTopThirdEntity currentThirdEntity) {
		mSelf2.updateThirdEntity(currentThirdEntity);
		mSelf2.updateThirdBtn(currentThirdEntity);
	}

}
